//
//  ChooseController.swift
//  DSP
//
//  Created by tucici on 2024/5/24.
//

import UIKit
import TCCKit
import SwiftyJSON
class ChooseController: DSAlertController {
    private var bgView : UIView!
    private var name : UILabel!
    private var line : UIView!
    private var click:Bool = false
    var block : ((_ model:ChannelModel)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    override func initUI() {
        bgView = UIView()
        bgView.bgHexColor = "#FFFFFF"
        bgView.cornerRadius = 10.k_h
        
        line = UIView()
        line.bgHexColor = "#000000"
        
        name = UILabel()
        name.text = "Choose what channel as a benchmark"
        name.font = font(18,.bold)
        name.textHexColor = "#000000"
        name.minimumScaleFactor = 0.5
        name.adjustsFontSizeToFitWidth = true
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.scrollDirection = .vertical
        flowlayout.minimumLineSpacing = 0.0
        flowlayout.minimumInteritemSpacing = 0.0
        flowlayout.itemSize = CGSize(width:kUIScreenWidth / 2.0 - 10 - k_w(30), height: 44)
        flowlayout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        collectionView.collectionViewLayout = flowlayout
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ChooseCell.self, forCellWithReuseIdentifier:ChooseCell.ID())
        
        view.addSubview(bgView)
        bgView.addSubview(name)
        bgView.addSubview(line)
        bgView.addSubview(collectionView)
        
        bgView.snp.makeConstraints { make in
            make.height.equalTo(k_h(217))
            make.left.right.equalToSuperview().inset(k_w(30))
            make.centerY.equalToSuperview()
        }
        
        name.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.height.equalTo(30)
            make.top.equalToSuperview()
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.height.equalTo(1)
            make.top.equalTo(name.snp.bottom)
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    private var array = [ChannelModel](){
        didSet{
            collectionView.reloadData()
        }
    }
    
    var group : GroupModel!{
        didSet{
            for ch in array{
                ch.enable = group.contain(ch: ch)
            }
            collectionView.reloadData()
        }
    }
    
    override func initData() {
        let names = ["CH1","CH2","CH3","CH4","CH5","CH6","CH7","CH8"]
        let jsons = names.map{JSON(["name":$0,"enable":true])}
        array = jsons.map{ChannelModel.json($0)}
        collectionView.reloadData()
    }
    
}

//MARK: - UICollectionViewDataSource
extension ChooseController:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:ChooseCell.ID(), for:indexPath) as! ChooseCell
        let model = array[indexPath.row]
        cell.isenable = group.contain(ch: model)
        cell.model = array[indexPath.row]
        cell.line2.isHidden = ((indexPath.row % 2) != 0)
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension ChooseController:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        let ch = array[indexPath.row]
        if ch.select || ch.enable == false || click {return}
        click = true
        ch.select = true
        collectionView.reloadData()
        block?(ch)
        CLHUD(type: .loading,time: 1, callBack: {[weak self] in
            self?.dismiss(animated: false)
            
        })
    }
}


